/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQueries( {
    @NamedQuery(name="findActiveActivities",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) k, Activity t WHERE s = :search AND t = k AND t.state.name <> 'Start State'"),
    @NamedQuery(name="findActivities",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) k, Activity t WHERE s = :search AND t = k")
})
public class Activity extends Task {
    private static final long serialVersionUID = 1L;

    private byte percentageComplete;

    private int expectedDuration_min;

    private int remainingDuration_min;

    @ManyToOne
    private TaskStateMachine taskStateMachine;

    @ManyToOne
    private State state;

    @ManyToOne
    private Status status;

    @OneToMany(mappedBy = "activity")
    private Collection<Time> times;

    public int getExpectedDuration_min() {
        return expectedDuration_min;
    }

    public void setExpectedDuration_min(int expectedDuration_min) {
        this.expectedDuration_min = expectedDuration_min;
    }

    public byte getPercentageComplete() {
        return percentageComplete;
    }

    public void setPercentageComplete(byte percentageComplete) {
        this.percentageComplete = percentageComplete;
    }

    public int getRemainingDuration_min() {
        return remainingDuration_min;
    }

    public void setRemainingDuration_min(int remainingDuration_min) {
        this.remainingDuration_min = remainingDuration_min;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public TaskStateMachine getTaskStateMachine() {
        return taskStateMachine;
    }

    public void setTaskStateMachine(TaskStateMachine taskStateMachine) {
        this.taskStateMachine = taskStateMachine;
    }

    public Collection<Time> getTimes() {
        return times;
    }

    public void setTimes(Collection<Time> times) {
        this.times = times;
    }

    public String expectedDurationToString() {
        return durationToString(expectedDuration_min);
    }

    public String remainingDurationToString() {
        return durationToString(remainingDuration_min);
    }

    public static String durationToString(int duration_min) {
        int minutes = duration_min % 60;
        int hours = (duration_min / 60) % 8;
        int days = duration_min / (8 * 60);
        return Integer.toString(days) + "day:" + Integer.toString(hours) + "hr:" + Integer.toString(minutes) + "min";
    }

    public int getTotalTime_min() {
        int totalTime_min = 0;
        for (Time time: times) {
            totalTime_min += time.getDuration() / 60000;
        }
        return totalTime_min;
    }

}
